#coding:utf-8
import time,serialCom
import os
import logging
from checkSum import hex_checksum
from str2arry import str2arry

FILE = os.getcwd()          #返回当前目录的路径
#logging基础配置，文件名为 os.path.join里面的两个值想加个，输出等级为≥DEBUG级别
logging.basicConfig(filename = os.path.join(FILE,'log.txt'),level = logging.DEBUG,
                    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s')

def clearEvent():
    print '----------------------------------------------------------'
    print 'case2 清除事件记录' + '\n' + '                                    '
    logging.debug('*****case 2 清除事件记录*****')
    # 清除事件记录格式
    clearEventFirst = 'FE FE FE FE FE 68 AB 57 58'
    # 表号
    clearEventMid = 'F2 77 88 99 55 01 00 03 92 00 01'

    #读取表号
    meterIDread = (
        ''.join(('FE FE FE FE FE 68 AB 57 58 AA AA AA AA AA F2 77 88 99 55 03 00 03 81 0A 01 85 16').split()))
    ID = serialCom.serialCom(meterIDread, 35)[28:38]
    ID = str2arry(ID)
    #读取表号
    # print ID
    Data = clearEventFirst.split() + ID + clearEventMid.split()
    # print Data
    checkSum = hex_checksum(Data[5:])
    send_Data = ''.join(Data + checkSum + ['16'])
    print '发送:%s'%send_Data
    receiveData = serialCom.serialCom(send_Data, 40)  # 发送清除事件记录指令，并接收返回数据
    print '接收:%s'%receiveData
    checkData = send_Data[:38] + '810005' + send_Data[-10:]
    checkDataArry = str2arry(checkData)
    receiveData_check = checkData + ''.join(hex_checksum(checkDataArry[5:])) + '16'
    # print receiveData_check

    if receiveData[:58] == receiveData_check:
        print '清除事件记录成功'
        logging.debug('清除事件记录成功')
    else:
        print '失败'
        logging.debug('清除事件记录失败')

